package com.chenjun.common.ctrlformat;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.Formatter;

/**
 * @Title EmailFormatter
 * @Description:
 * @author 李伟
 * @date 2015年7月31日 上午11:11:18
 */
public class ListFormatter implements Formatter<List<String>> {

	private String pattern = "";

	public ListFormatter(String pattern) {
		if (pattern != null) {
			this.pattern = pattern;
		}
	}

	/* 
	 * @see org.springframework.format.Printer#print(java.lang.Object, java.util.Locale)
	 */
	@Override
	public String print(List<String> object, Locale locale) {
		return object == null ? null : object.toString();
	}

	/* 
	 * @see org.springframework.format.Parser#parse(java.lang.String, java.util.Locale)
	 */
	@Override
	public List<String> parse(String text, Locale locale) throws ParseException {
		if (text == null) {
			return null;
		}
		List<String> list = new ArrayList<String>();
		String[] split = text.split(this.pattern);
		for (String str : split) {
			if (!StringUtils.isBlank(str)) {
				list.add(str);
			}
		}
		return list;
	}

}
